package com.crazy.bookstore.controller;

import com.crazy.bookstore.common.ApiRestResponse;
import com.crazy.bookstore.entity.Member;
import com.crazy.bookstore.entity.MemberReadState;
import com.crazy.bookstore.exception.ExceptionEnum;
import com.crazy.bookstore.exception.MyException;
import com.crazy.bookstore.service.MemberService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author: 卢辉宇
 * @date: 2022/12/29 20:30
 * @description:
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Resource
    MemberService memberService;

    /**
     *
     * @param username 用户名
     * @param password 密码
     * @param nickname 昵称
     * @param vc 验证码
     * @param request 会话
     * @return
     */
    @PostMapping("/register")
    public ApiRestResponse register(String username, String password, String nickname, String vc, HttpServletRequest request) throws MyException {

        //检查验证码
        checkVerifyCode(vc,request);

        /*调用service层的注册逻辑。。。*/
        Member member = memberService.createMember(username, password, nickname);


        return ApiRestResponse.seccess(member);
    }

    /**
     * 对检查验证码进行封装
     * @param vc 用户输入的验证码
     * @param request 前端会话
     * @throws MyException
     */
    private void checkVerifyCode(String vc, HttpServletRequest request) throws MyException {
        //从会话中拿到当前存储的验证码
        String verifyCode = (String)request.getSession().getAttribute("kaptchaVerifyCode");
        if(vc==null||!vc.equalsIgnoreCase(verifyCode)){
            /*验证码错误*/
            throw new MyException(ExceptionEnum.VERIFY_CODE_ERROR);
        }
    }

    /**
     * 检查登录
     * @param username
     * @param password
     * @param vc
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("/login")
    public ApiRestResponse checkLogin(String username, String password, String vc, HttpServletRequest request) throws MyException {
        //检查验证码
        checkVerifyCode(vc,request);

        /*调用service层的登录逻辑。。。*/
        Member member = memberService.checkLogin(username, password);

        return ApiRestResponse.seccess(member);
    }

    @GetMapping("/select_by_id")
    public ApiRestResponse selectById(Long memberId){
        //调用service。。。
        Member member = memberService.selectById(memberId);

        return ApiRestResponse.seccess(member);
    }

    @GetMapping("/select_member_read_state")
    public ApiRestResponse selectMemberReadState(Long memberId,Long bookId){

        MemberReadState memberReadState = memberService.selectMemberReadState(memberId, bookId);

        return ApiRestResponse.seccess(memberReadState);
    }

    /**
     * 更新阅读状态
     * @param memberId
     * @param bookId
     * @param readState
     * @return
     */
    @PostMapping("/update_read_state")
    public ApiRestResponse updateReadState(Long memberId,Long bookId,Integer readState){
        MemberReadState memberReadState = memberService.updateMemberReadState(memberId, bookId, readState);

        return ApiRestResponse.seccess(memberReadState);
    }
}
